<!--
 * @Author: your name
 * @Date: 2022-02-19 11:05:24
 * @LastEditTime: 2022-02-19 11:17:04
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\969.煎饼排序.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
 * @param {number[]} arr
 * @return {number[]}
 */
        var pancakeSort = function (arr) {
            let result = [], al = arr.length, isEND = false;
            while (!isEND) {
                for (let i = 0; i < al; i++) {
                    if (i === al - 1) {
                        isEND = true;
                        break;
                    }
                    let num;
                    for (let j = i + 1; j < al; j++) {
                        if (arr[j] === arr[i] + 1) {
                            num = j;
                            break;
                        }
                    }
                    if (num) {
                        if (num !== i + 1) { //翻转
                            result.push(num);
                            let newarr = arr.slice(i, num);
                            arr = newarr.reverse().concat(arr.slice(num));
                            break;
                        }
                    } else {
                        arr = arr.reverse();
                        break;
                    }
                }
            }
            return arr;
        };
        console.log(pancakeSort([3,2,4,1])); 
    </script>
</body>

</html>